package com.sat.goods.controller;

import com.sat.goods.pojo.Category;
import com.sat.goods.pojo.Goods;
import com.sat.goods.pojo.GoodsVo;
import com.sat.goods.service.GoodsService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodService;

    /**
     * 回滚库存
     * @param id
     * @param num
     * @return
     */
    @RequestMapping("/rollBackSkuStock")
    public Result<Integer> rollBackSkuStock(Long id,Integer num){
        Integer skuNum = goodService.rollBackSkuStock(id,num);
        return new Result<Integer>(true, StatusCode.OK,"回滚库存成功",num);
    }

    /**
     * 减少库存
     * @param id
     * @return
     */
    @RequestMapping("/updateSkuNum")
    public Result<Integer> updateSkuNum(Long id,Integer num){
        Integer skuNum = goodService.updateSkuNum(id,num);
        return new Result<Integer>(true, StatusCode.OK,"减少库存成功",num);
    }

    /**
     * 通过skuid查找sku和spu，并返回两者的包装类GoodsVo
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<GoodsVo> findById(@PathVariable Long id){
        GoodsVo goodsVo = goodService.findById(id);
        return new Result<GoodsVo>(true, StatusCode.OK,"查询成功",goodsVo);
    }

    /**
     * spu+sku组合成商品表，前端会把spu和sku数据传过来
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){

        goodService.saveGoods(goods);
        return new Result<List<Category>>(true, StatusCode.OK,"保存成功！");
    }

    /**
     * 下架商品spu
     * @param id
     * @return
     */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable Long id){
        goodService.pull(id);
        return new Result(true,StatusCode.OK,"下架成功");
    }

    @PutMapping("/put/many")
    public Result putMany(@RequestBody Long[] ids){
        int count = goodService.putMany(ids);
        return new Result(true,StatusCode.OK,"批量上架"+count+"商品");
    }

}
